package com.anyikang.volunteer.sos.volunteer.app.bean;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class User {


    //现在是以userPhone为账号名登录的
    private String userName;
    private String password;
    private String userId;
    private Context mContext;
    private String userToken;
    private String headPicPath;



    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getHeadPicPath() {
        return headPicPath;
    }

    public void setHeadPicPath(String headPicPath) {
        this.headPicPath = headPicPath;
    }

    public String getUserToken() {
        return userToken;
    }

    public void setUserToken(String userToken) {
        this.userToken = userToken;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public User()
    {
        ;
    }

    /**
     * 读取用户信息
     * @param context
     */
    public User(Context context)
    {
        this.mContext = context;
        SharedPreferences sp = mContext.getSharedPreferences("login",Context.MODE_PRIVATE);
        this.userId = sp.getString("userId", "");
        this.password = sp.getString("password","");
        this.userName = sp.getString("userName","");
        this.userToken = sp.getString("userToken","");
        this.headPicPath = sp.getString("headPicPath","");
    }


    /**
     * 保存用户信息
     * @param context
     * @param user： 用户信息的各项值都在ContentValues里
     */
    public void saveUserInfo(Context context ,ContentValues user)
    {
        if(user != null)
        {
            SharedPreferences loginPreference = context.getSharedPreferences("login", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = loginPreference.edit();

            String userId = user.getAsString("userId");
            String password = user.getAsString("password");
            String userName = user.getAsString("userName");
            String userToken = user.getAsString("userToken");
            String headPicPath = user.getAsString("headPicPath");

            //可以一次保存一个值或多个值，取决于ContentValues里的各个值是否为空
            if(!TextUtils.isEmpty(userId))
            {
                edit.putString("userId", userId);
            }

            if(!TextUtils.isEmpty(password))
            {
                edit.putString("password", password);
            }

            if(!TextUtils.isEmpty(userName))
            {
                edit.putString("userName", userName);
            }

            if(!TextUtils.isEmpty(userToken))
            {
                edit.putString("userToken", userToken);
            }

            if(!TextUtils.isEmpty(headPicPath))
            {
                edit.putString("headPicPath", headPicPath);
            }

            edit.commit();
        }
    }

    /**
     *  用户退出登录，清除所有用户信息
     * @param context
     *
     */
    public void userLogOut(Context context,int type)
    {
        SharedPreferences loginPreference = context.getSharedPreferences("login", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = loginPreference.edit();
        edit.putString("userId", "");
        //这里存储的是未加密的源码，以后登录时候要给密码加密。先这样测试几天，然后再存储成加密后的密码，以后登录就不需要再加密了

      //gxw-  if(type == 1)   //修改密码时要清空
        {
            edit.putString("password","");
        }


        //gxw-edit.putString("userName","");  退出登录时不清除用户名，是为了下次登录方便（不用填写用户名）
        edit.putString("userToken","");
        edit.putString("headPicPath","");
        edit.commit();
    }

    /**
     * 判断是否已登录（记住登录）
     * @return
     */
    public boolean isLogin() {

        if(!TextUtils.isEmpty(userId))
        {
            return true;
        }
        return false;
    }

}
